﻿using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.XMLConfigModel.DataStores;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LdtDataEngin.ModuleDetail.BaseModule.Optimization
{
    public class QuantityOptimizationData : SgingOPtimizationBase
    {
        public QuantityOptimizationData(DataContext context)
        {
            dataContext = context;
        }

        /// <summary>
        /// 执行优选
        /// </summary>
        /// <param name="dataModelAsis">分析数据源</param>
        /// <param name="dataModelOP">优选数据源</param>
        /// <param name="opsum">优选数据集</param>
        /// <param name="assum">分析数据集</param>
        /// <param name="xmllableConfig">配置</param>
        /// <param name="soursnameopid">数据源id</param>
        /// <param name="soursnameasisid">数据源id</param>
        public void Optimization(DataModel dataModelAsis, DataModel dataModelOP, int opsum, int assum, Xmllable xmllableConfig, string soursnameopid, string soursnameasisid)
        {
            string sourCode = dataModelAsis.sourceData.sourceList[0].SourceCode;

            LdtJudge.DataEngine.Model.XMLConfigModel.DataStores.Source sourceNodeOP = dataModelOP.sourceData.sourceList.FirstOrDefault(a => a.SourceCode.ToLower() == sourCode.ToLower());
            if (sourceNodeOP != null)
            {
                sourceNodeOP = dataModelAsis.sourceData.sourceList[0];
            }
            else
            {
                Source sourceopdata = dataModelAsis.sourceData.sourceList[0];
                sourceopdata.IsCheck = "0";
                dataModelOP.sourceData.sourceList.Add(sourceopdata);
            }
            if (assum <= opsum)
            {
                foreach (Source sourceNode in dataModelOP.sourceData.sourceList)
                {
                    sourceNode.AnalysisStatus = "1";
                    if (sourceNode.SourceCode.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower())
                    {
                        sourceNode.IsCheck = "1";
                    }
                    else
                    {
                        sourceNode.IsCheck = "0";
                    }
                }
            }

            //循环比较属性信息
            foreach (AttributeInfo attritem in dataModelAsis.AttributesList)
            {
                AttributeInfo attributeop = dataModelOP.AttributesList.FirstOrDefault(a => a.Name == attritem.Name);
                if (attributeop == null)
                {
                    AttributeInfo attributeTmp = new AttributeInfo();
                    attributeTmp.Name = attritem.Name;
                    attributeTmp.DictionariesCode = attritem.DictionariesCode;
                    attributeTmp.Priority = attritem.Priority;
                    attributeTmp.sourceList = attritem.sourceList;
                    dataModelOP.AttributesList.Add(attributeTmp);
                }
                else
                {
                    if (attributeop.sourceList.FirstOrDefault(a => a.SourceCode.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower()) == null)
                    {
                        Source attsoucremodel = attritem.sourceList[0];
                        attsoucremodel.IsCheck = "0";
                        attributeop.sourceList.Add(attsoucremodel);
                    }
                    if (xmllableConfig.ElementList != null)
                    {
                        Element element = xmllableConfig.ElementList.FirstOrDefault(a => a.Name == attritem.Name);
                        if (element != null && !string.IsNullOrEmpty(element.Priority))
                        {
                            opsum = ListByCount(element.Priority.Split('>').ToList(), soursnameopid);
                            assum = ListByCount(element.Priority.Split('>').ToList(), soursnameasisid);
                        }
                    }

                    if (assum < opsum)
                    {
                        foreach (LdtJudge.DataEngine.Model.XMLConfigModel.DataStores.Source attsource in attributeop.sourceList)
                        {
                            attsource.AnalysisStatus = "1";
                            if (attsource.SourceCode.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower())
                            {
                                attsource.IsCheck = "1";
                            }
                            else
                            {
                                attsource.IsCheck = "0";
                            }
                        }
                    }
                    else if (assum == opsum)
                    {
                        try
                        {


                            LdtJudge.DataEngine.Model.XMLConfigModel.DataStores.Source sourceasis = attritem.sourceList.FirstOrDefault(a => a.SourceCode.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower());
                            LdtJudge.DataEngine.Model.XMLConfigModel.DataStores.Source sourceop = attributeop.sourceList.FirstOrDefault(a => a.SourceCode.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower());
                            attributeop.sourceList.Remove(sourceop);
                            attributeop.sourceList.Add(sourceasis);
                        }
                        catch (Exception ef)
                        {
                           

                        }
                    }

                    if (assum <= opsum)
                    {
                        foreach (Source sourceNode in attributeop.sourceList)
                        {
                            sourceNode.AnalysisStatus = "1";
                            if (sourceNode.SourceCode.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower())
                            {
                                sourceNode.IsCheck = "1";
                            }
                            else
                            {
                                sourceNode.IsCheck = "0";
                            }
                        }
                    }
                }
            }
        }


        /// <summary>
        /// 执行优选操作
        /// </summary>
        /// <param name="dataModelAsisList">分析完整结果</param>
        /// <param name="dataModelsOPList">优选完整结果</param>
        /// <param name="analysisModel">分析实体</param>
        /// <param name="OptimizationResultsList">优选条件数据</param>
        public void OptimizationData(List<DataModel> dataModelAsisList, List<DataModel> dataModelsOPList, DataModel analysisModel, List<DataModel> OptimizationResultsList)
        {
            foreach (DataModel asisModel in dataModelAsisList)
            {
                DataModel sginDataModel = OptimizationResultsList[0];

                string opSoursNumber = Sourcedatadefine[0].SourcedataList.FirstOrDefault(a => a.Code == sginDataModel.sourceData.sourceList.FirstOrDefault(a => a.IsCheck == "1").SourceCode).id.ToString();
                string analysisNumber = Sourcedatadefine[0].SourcedataList.FirstOrDefault(a => a.Code == dataContext.DataLayerAns.SourceCode).id.ToString();
                int opNumber = ListByCount(xmlConfig.Priority.Split('>').ToList(), opSoursNumber);
                int analyNumber = ListByCount(xmlConfig.Priority.Split('>').ToList(), analysisNumber);
                bool determine = true;

                #region 节点优选数据
                //分析数据优先级高
                if (analyNumber < opNumber)
                {
                    if (sginDataModel.sourceData.sourceList.FirstOrDefault(a => a.SourceCode.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower()) != null)
                    {
                        foreach (Source sginsoure in sginDataModel.sourceData.sourceList)
                        {
                            if (sginsoure.SourceCode.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower())
                            {
                                sginsoure.IsCheck = "1";
                            }
                            else
                            {
                                sginsoure.IsCheck = "0";
                            }
                        }
                    }
                    else
                    {
                        Source source = analysisModel.sourceData.sourceList[0];
                        foreach (Source sginsoure in sginDataModel.sourceData.sourceList)
                        {
                            sginsoure.IsCheck = "0";
                        }
                        sginDataModel.sourceData.sourceList.Add(source);
                    }
                }
                //结果数据优先级高
                if (analyNumber > opNumber)
                {
                    if (sginDataModel.sourceData.sourceList.FirstOrDefault(a => a.SourceCode.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower()) == null)
                    {
                        Source source = analysisModel.sourceData.sourceList[0];
                        sginDataModel.sourceData.sourceList.Add(source);
                    }
                }
                //双方优先级相等
                if (analyNumber == opNumber)
                {
                    Source ansislySoure = analysisModel.sourceData.sourceList[0];
                    Source opSoure = sginDataModel.sourceData.sourceList.FirstOrDefault(a => a.SourceCode.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower());
                    string ischck = opSoure.IsCheck;
                    ansislySoure.IsCheck = ischck;
                    opSoure = ansislySoure;
                }
                #endregion

                #region 属性优选
                foreach (AttributeInfo asisylAttribute in analysisModel.AttributesList)
                {
                    if (sginDataModel.AttributesList.FirstOrDefault(a => a.Name == asisylAttribute.Name && a.Name != "id") == null)
                    {
                        AttributeInfo attributeTmp = new AttributeInfo();
                        attributeTmp.Name = asisylAttribute.Name;
                        attributeTmp.DictionariesCode = asisylAttribute.DictionariesCode;
                        attributeTmp.Priority = asisylAttribute.Priority;
                        attributeTmp.sourceList = asisylAttribute.sourceList;
                        sginDataModel.AttributesList.Add(attributeTmp);
                    }
                    else
                    {
                        AttributeInfo attributeop = sginDataModel.AttributesList.FirstOrDefault(a => a.Name == asisylAttribute.Name && a.Name != "id");
                        if (attributeop.sourceList.FirstOrDefault(a => a.SourceCode.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower()) == null)
                        {
                            Source attsoucremodel = asisylAttribute.sourceList[0];
                            attsoucremodel.IsCheck = "0";
                            attributeop.sourceList.Add(attsoucremodel);
                        }
                        if (xmlConfig.ElementList != null)
                        {
                            Element element = xmlConfig.ElementList.FirstOrDefault(a => a.Name == asisylAttribute.Name);
                            if (element != null && !string.IsNullOrEmpty(element.Priority))
                            {
                                if (element.ConditionList.Count > 0)
                                {

                                    try
                                    {
                                        determine = ConditionDetermine(element.ConditionList);
                                    }
                                    catch (System.Exception ef)
                                    {
                                        dataContext.InteractiveShow.LogShow("当前数据项条件解析错误，配置信息：" + JsonConvert.SerializeObject(ef.ToString()));
                                        dataContext.InteractiveShow.LogShow("当前数据项条件解析错误，错误信息：" + ef.ToString());
                                    }



                                }
                                opNumber = ListByCount(element.Priority.Split('>').ToList(), opSoursNumber);
                                analyNumber = ListByCount(element.Priority.Split('>').ToList(), analysisNumber);
                            }
                        }


                        if (!determine)
                        {
                            return;
                        }
                        if (analyNumber < opNumber)
                        {
                            foreach (Source attsource in attributeop.sourceList)
                            {

                                if (attsource.SourceCode.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower())
                                {
                                    attsource.IsCheck = "1";
                                }
                                else
                                {
                                    attsource.IsCheck = "0";
                                }
                            }
                        }
                        else if (analyNumber == opNumber)
                        {
                            try
                            {


                                LdtJudge.DataEngine.Model.XMLConfigModel.DataStores.Source sourceasis = asisylAttribute.sourceList.FirstOrDefault(a => a.SourceCode.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower());
                                LdtJudge.DataEngine.Model.XMLConfigModel.DataStores.Source sourceop = attributeop.sourceList.FirstOrDefault(a => a.SourceCode.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower());
                                attributeop.sourceList.FirstOrDefault(a => a.SourceCode.ToLower() == dataContext.DataLayerAns.SourceCode.ToLower()).SaveValue = sourceasis.SaveValue;
                                attributeop.sourceList.Remove(sourceop);
                                attributeop.sourceList.Add(sourceasis);
                            }
                            catch (System.Exception)
                            {
                                
                            }
                        }
                    }

                }
                #endregion
            }
        }
    }
}
